<?php
namespace app\shop\model;
use app\rbac\model\AdminM;
use think\Validate;
use think\Db;
use app\base\model\Base;
class GoodsattrM extends Base
{

       protected function _V($scene,$data)
       {
           $rule = [
               'goods_attr_id' => 'require',
               'goods_id' => 'require|number',
               'goods_num' => 'require|number',
               'goods_price' => 'require|number',
               'promote_price' => 'require|number',
               'member_price' => 'require|number',
               'goods_weight' => 'require|number',

           ];
        $msg = [];
        $filed = [
            'goods_attr_id'=>'商品属性',
            'goods_id'=>'商品ID',
            'goods_number'=>'商品数量',
            'goods_price'=>'商品售价',
            'promote_price'=>'促销价格',
            'member_price'=>'会员价格',
            'goods_weight'=>'商品重量',

        ];
           $validate = new Validate($rule,$msg,$filed);
           $validate->scene('add', ['goods_attr_id','goods_id','goods_number','goods_price','promote_price','member_price','goods_weight']);
           $validate->scene('edit', ['goods_id','goods_number','goods_price','promote_price','member_price','goods_weight']);
           $result = $validate->scene($scene)->check($data);
           if($result === false)
           {
               return $validate->getError();
           }
           else
           {
               return true;
           }

       }

    /**
     * 新增数据
     * @return mixed
     */

    public function add($data)
    {
        //验证数据
        $res = $this->_V('add',$data);
        if($res !== true)
        {
            $this->code = -1;
            $this->msg = $res;
            return false;
        }
        //判断属性是否存在
        $goods_attr_id = trim(implode(',',$data['goods_attr_id']),',');
        if(empty($goods_attr_id))
        {
            $this->code = -1;
            $this->msg = '属性不能为空';
            return false;
        }
        $gaids = Db::table('shop_goods_num')->where(['goods_id'=>$data['goods_id'],'is_usable'=>1])->column('goods_attr_id');
        foreach($gaids as $v)
        {
            if($v == $goods_attr_id)
            {
                $this->code = -1;
                $this->msg = '属性已经存在';
                return false;
            }
        }
        //插入数据
        $insertData = [];
        $insertData['admin_id'] = session('admin_id');
        //判断提交了几个属性
        $insertData['goods_number']=$data['goods_number'];
        $insertData['goods_id']=$data['goods_id'];
        $insertData['goods_attr_id']=$goods_attr_id;
        $insertData['goods_price']=$data['goods_price'];
        $insertData['promote_price']=$data['promote_price'];
        $insertData['member_price']=$data['member_price'];
        $insertData['goods_weight']=$data['goods_weight'];

        //处理图片
        $ret = uploadOne('goods_pic', array(
            array(600, 600),
            array(100, 100),
        ));
        if($ret['ok'] == 1)
        {
            $insertData['goods_pic'] = $ret['images'][0];   // 原图地址
            $insertData['goods_big_pic'] = $ret['images'][1];   // 第一个缩略图地址
            $insertData['goods_sm_pic'] = $ret['images'][2];   // 第二个缩略图地址
        }
        else if($ret['ok'] == 2)
        {
            $this->code = -1;
            $this->msg = '商品属性图片必须上传';
            return false;
        }
        else
        {
            $this->code = -1;
            $this->msg = $ret['error'];
            return false;
        }

            $res = Db::table('shop_goods_num')->insertGetId($insertData);
        if(!$res)
        {
            $this->code = -1;
            $this->msg = '添加失败';
            return false;
        }
        return true;

    }


    /**
     * 删除数据
     * @param $id
     */
    public function del($id)
    {
        return Db::table('shop_goods_num')->where('id',$id)->update(['is_usable'=>0]);
    }




    /**
     * 商品属性
     * @param $goods_id
     * @return array
     */
    public function getGoodsAttrByGoodsID($goods_id){

        $info = Db::table('shop_goods_attr')
                ->alias('ga')
                ->join('shop_attribute a','a.id = ga.attr_id')
                ->field('a.attr_name,ga.id,ga.attr_value')
                ->where(['ga.goods_id'=>$goods_id,'a.attr_type'=>'可选'])
                ->select();
        $data=array();
        foreach($info as $k=>$v){//把相同的名称的属性值放到一个数组
            $data[$v['attr_name']][]=$v;
        }
        return $data;
    }

    public function goods_num($data)
    {
        //验证数据
        $res = $this->_V('edit',$data);
        if($res !== true)
        {
            $this->code = -1;
            $this->msg = $res;
            return false;
        }
        //插入数据
        $insertData = [];
        $insertData['admin_id'] = session('admin_id');
        //判断提交了几个属性
        $insertData['goods_number']=$data['goods_number'];
        $insertData['goods_id']=$data['goods_id'];
        $insertData['goods_price']=$data['goods_price'];
        $insertData['promote_price']=$data['promote_price'];
        $insertData['member_price']=$data['member_price'];
        $insertData['goods_weight']=$data['goods_weight'];

        //处理图片
        $ret = uploadOne('goods_pic', array(
            array(600, 600),
            array(100, 100),
        ));
        if($ret['ok'] == 1)
        {
            $insertData['goods_pic'] = toQiNiu($ret['images'][0]);   // 原图地址
            $insertData['goods_big_pic'] = toQiNiu($ret['images'][1]);   // 第一个缩略图地址
            $insertData['goods_sm_pic'] = toQiNiu($ret['images'][2]);   // 第二个缩略图地址
        }
        else if($ret['ok'] == 2)
        {
        }
        else
        {
            $this->code = -1;
            $this->msg = $ret['error'];
            return false;
        }

        $res = Db::table('shop_goods_num')->where(['id'=>$data['id']])->update($insertData);
        if(!$res)
        {
            $this->code = -1;
            $this->msg = '修改失败';
            return false;
        }
        return true;

    }


}
